/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2024 All Rights Reserved.
 */
package com.alipay.demo.controller;

import com.alipay.api.domain.LocalAppxCategoryVO;
import com.alipay.api.response.AlipayOpenAppLocalitemListQueryResponse;
import com.alipay.api.response.AlipayOpenAppLocalitemQueryResponse;
import com.alipay.api.response.AlipayOpenAppLocalitemTemplateQueryResponse;
import com.alipay.demo.service.LocalItemService;
import com.alipay.demo.vo.LocalItemCreateRequest;
import com.alipay.demo.vo.OpenResponse;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jishupei.jsp
 * @version : LocalItemController, v0.1 2024年05月21日 4:48 下午 jishupei.jsp Exp $
 */
@Api(tags = "本地商品模块")
@ApiSupport(order = 3)
@RestController
@RequestMapping(value = "/localitem")
public class LocalItemController {

    @Autowired
    private LocalItemService localItemService;

    /**
     * 类目查询
     * 获取叶子节点商品类目列表（仅获取类目状态为资质已开通的）
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "类目查询", notes = "获取叶子节点商品类目列表（仅获取类目状态为资质已开通的）")
    @GetMapping(value = "/category/list")
    public OpenResponse<List<LocalAppxCategoryVO>> categoryList(
            @ApiParam(value = "商品类型：1-团购券，2-代金券，3-次卡，7-日历房", required = true) @RequestParam String itemType) {
        return localItemService.getCategories(itemType);
    }

    /**
     * 本地商品模板信息查询
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "本地商品模板信息查询")
    @GetMapping(value = "/template/query")
    public OpenResponse<AlipayOpenAppLocalitemTemplateQueryResponse> queryTemplate(
            @ApiParam(value = "类目ID", required = true) @RequestParam String categoryId,
            @ApiParam(value = "商品类型：1-团购券，2-代金券，3-次卡，7-日历房", required = true) @RequestParam String itemType) {
        return localItemService.queryTemplate(categoryId, itemType);
    }

    /**
     * 本地商品创建
     */
    @ApiOperation(value = "本地商品（团购券、代金券、次卡）创建")
    @ApiOperationSupport(order = 3)
    @PostMapping(value = "/create")
    public OpenResponse<String> localItemCreate(@ApiParam(value = "商品类型：1-团购券，2-代金券，3-次卡", required = true) @RequestParam String itemType,
                                                       @RequestBody LocalItemCreateRequest request) {
        return localItemService.createLocalItem(itemType, request);
    }

    /**
     * 日历房本地商品创建
     */
    @ApiOperation(value = "日历房本地商品创建")
    @ApiOperationSupport(order = 4)
    @PostMapping(value = "/calendar/create")
    public OpenResponse<String> calendarLocalItemCreate(@RequestBody LocalItemCreateRequest request) {
        return localItemService.createCalendarLocalItem(request);
    }

    /**
     * 修改团购券商品
     */
    @ApiOperation(value = "本地商品（团购券、代金券、次卡）修改")
    @ApiOperationSupport(order = 5)
    @PostMapping(value = "/update")
    public OpenResponse<String> localItemUpdate(@ApiParam(value = "商品类型：1-团购券，2-代金券，3-次卡", required = true) @RequestParam String itemType,
                                                @RequestBody LocalItemCreateRequest request) {
        return localItemService.updateLocalItem(itemType, request);
    }

    /**
     * 本地商品详情查询
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "本地商品详情查询")
    @GetMapping(value = "/query")
    public OpenResponse<AlipayOpenAppLocalitemQueryResponse> queryItemDetail(
            @ApiParam(value = "支付宝侧商品id") @RequestParam(required = false) String itemId,
            @ApiParam(value = "商家侧商品id") @RequestParam(required = false) String outItemId,
            @ApiParam(value = "是否查询编辑版本，0-在线版本，1-编辑版本（默认）") @RequestParam(required = false) String needEditSpu) {
        return localItemService.queryItemDetail(itemId, outItemId, needEditSpu);
    }

    /**
     * 本地商品分页查询
     */
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "本地商品分页查询")
    @GetMapping(value = "/list")
    public OpenResponse<AlipayOpenAppLocalitemListQueryResponse> queryItemList(
            @ApiParam(value = "页码", defaultValue = "1") Long pageNum,
            @ApiParam(value = "单页大小", defaultValue = "10") Long pageSize,
            @ApiParam(value = "平台侧商品id列表，逗号隔开") @RequestParam(required = false) String itemIds,
            @ApiParam(value = "商家侧id列表，逗号隔开") @RequestParam(required = false) String outItemIds,
            @ApiParam(value = "商品SPU状态") @RequestParam(required = false) String spuStatus
    ) {
        List<String> itemIdList = StringUtils.isBlank(itemIds)
                ? null : Lists.newArrayList(StringUtils.split(itemIds, ","));
        List<String> outItemIdList = StringUtils.isBlank(outItemIds)
                ? null : Lists.newArrayList(StringUtils.split(outItemIds, ","));
        return localItemService.queryItemList(pageNum, pageSize, itemIdList, outItemIdList, spuStatus);
    }
}
